package com.simple.cloud.core.base;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.io.Serializable;

import static com.simple.cloud.core.consts.CommonCst.*;


/**
 * 响应结构体
 */
@ApiModel(value = "响应结构体")
@ToString
@Accessors(chain = true)
@Data
public class RestResp<T> implements Serializable {
    @ApiModelProperty(value = "响应码：200，成功；")
    private int code = 200;
    @ApiModelProperty(value = "响应提示信息")
    private String msg = "操作成功";
    @ApiModelProperty(value = "响应数据")
    private T data;


    private RestResp() {
    }

    public static <T> RestResp<T> ok() {
        return setResult(null, RESP_SUCCESS, RESP_SUCCESS_MSG);
    }

    public static <T> RestResp<T> ok(T data) {
        return setResult(data, RESP_SUCCESS, RESP_SUCCESS_MSG);
    }

    public static <T> RestResp<T> ok(T data, String msg) {
        return setResult(data, RESP_SUCCESS, msg);
    }

    public static <T> RestResp<T> fail() {
        return setResult(null, RESP_FAIL, RESP_FAIL_MSG);
    }

    public static <T> RestResp<T> fail(String msg) {
        return setResult(null, RESP_FAIL, msg);
    }

    public static <T> RestResp<T> fail(T data) {
        return setResult(data, RESP_FAIL, RESP_FAIL_MSG);
    }

    public static <T> RestResp<T> fail(int status) {
        return setResult(null, status, RESP_FAIL_MSG);
    }

    public static <T> RestResp<T> fail(int status, T data) {
        return setResult(data, status, RESP_FAIL_MSG);
    }

    public static <T> RestResp<T> fail(int status, String msg) {
        return setResult(null, status, msg);
    }

    public static <T> RestResp<T> fail(T data, String msg) {
        return setResult(data, RESP_FAIL, msg);
    }

    private static <T> RestResp<T> setResult(T data, int status, String msg) {
        RestResp<T> apiRestResp = new RestResp<>();
        apiRestResp.setCode(status);
        apiRestResp.setData(data);
        apiRestResp.setMsg(msg);
        return apiRestResp;
    }


}
